package cn.ulyer.core.config;


import cn.ulyer.core.properties.SwaggerProperty;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperty.class)
@ConditionalOnProperty(name = "swagger.enable",havingValue = "true")
public class SwaggerConfig  {


    public SwaggerConfig(){
        System.out.println("enable swagger config");
    }

    @Autowired
    private SwaggerProperty swaggerProperty;



    @Bean
    public Docket docket(){
       return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select().apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any())
                .build();
    }


    public ApiInfo apiInfo (){
        return new ApiInfoBuilder().title(swaggerProperty.getTitle())
                .version(swaggerProperty.getVersion())
                .description(swaggerProperty.getDescription()).build();
    }



}
